package com.sias.controller;

import com.sias.annotation.LoginRequired;
import com.sias.model.User;
import com.sias.spring.service.UserService;
import com.sias.utils.RedisUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 吴文杰
 * @version 1.0
 * @createTime 2023-04-30 23:47:55
 */
@RestController
public class UserController {

  @Resource
  UserService userService;

  @Resource
  RedisUtil redisUtil;

  @GetMapping("/test/{id}")
  @LoginRequired
  public User test(@PathVariable("id") long id) {
    return userService.test(id);
  }

  @GetMapping("/redis/set")
  public String redisSet() {
    redisUtil.set("name","吴文杰",60);
    return "存储完成";
  }

  @GetMapping("/redis/get")
  public String redisGet() {
    String name = (String) redisUtil.get("name");
    if (name == null) return "验证码过期！";
    return name;
  }

  @GetMapping("/login/{username}/{password}")
  public String redisGet(@PathVariable("username") String username,
                         @PathVariable("password") String password,
                         HttpServletRequest request) {
    HttpSession session = request.getSession();
    session.setAttribute("username",username);
    return username;
  }
}
